import 'package:get/get.dart';
import 'package:flutter/widgets.dart';
import 'package:banquanjie_app/app/services/HttpsRequest.dart';
import 'package:banquanjie_app/app/widget/common/dropdownMenu.dart';
class IpEliteController extends GetxController {
  //TODO: Implement IpEliteController
 //TODO: Implement ProjectListController
  final DropDownMenuController dropDownMenuController = DropDownMenuController();

  final HttpsClient httpsClient = HttpsClient();
  final RxMap<String, dynamic> query = <String, dynamic>{
    "sortType": 3, // 3 最新  2推荐
    "province": '',
    'occupationIds': '',
    "pageSize": 10,
    "pageCode": 1
  }.obs;
  final RxMap<String, dynamic> tempQuery = <String, dynamic>{}.obs;
  final RxMap dicList = {
    'occupationIdsList': [],
    'provinceList': [],
  }.obs;
  final RxList ipEliteList = [].obs;
  final ScrollController scrollController = ScrollController();
  bool flag = false;
  final RxInt total = RxInt(-1);
  

  @override
  void onInit() {
    super.onInit();
    tempQuery.value = Map<String, dynamic>.from(query);
    getOccupationIdsList();
    getCityList();
    fnGet();
    scrollController.addListener(() {
      if (scrollController.position.pixels > scrollController.position.maxScrollExtent - 100) {
        if (flag == false && total.value > ipEliteList.length) {
          query['pageCode'] += 1;
          flag = true;
          fnGet();
        }
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.dispose();
    dropDownMenuController.close();
    super.onClose();
  }

 

  void getOccupationIdsList() async {
    var res = await httpsClient.get('/user/dic/IpOccupation');
    var result = res.data;
    if (result['code'] == 200) {
      dicList['occupationIdsList'] = result['data'];
      update();
    }
  }
  void getCityList() async {
    var res = await httpsClient.get('/user/dic/areaAll');
    var result = res.data;
    if (result['code'] == 200) {
      dicList['provinceList'] = result['data'];
      dicList['provinceList'].insert(0, { "id": '', "areaName": '全部'});
      update();
    }
  }

  setSearchQueryValue(key, value) {
    if (key != 'sortType' && tempQuery[key] == value) {
      tempQuery[key] = '';
    } else {
      tempQuery[key] = value;
    }
    update();
    // ipEliteList.value = [];
    // fnGet();
  }
  onSearch() {
    // query.assignAll(tempQuery);
    query.value = Map<String, dynamic>.from(tempQuery);
    ipEliteList.value = [];
    query['pageCode'] = 1;
    fnGet();
    dropDownMenuController.close();
  }
  onClearCondition() {
    query['occupationIds'] = '';
    query['province'] = '';
    query['pageCode'] = 1;
    ipEliteList.value = [];
    fnGet();
    dropDownMenuController.close();
  }
  closeSearchMask() {
    tempQuery.value = Map<String, dynamic>.from(query);
  }
  void fnGet() async{
    var res = await httpsClient.post('/user/up/xcxRegisterList', query);
     var result = res.data;
    if (result['code'] == 200) {
      ipEliteList.addAll(result['data']['rows']);
      total.value = result['data']['total'];
      flag = false;
      update();
    }
  }

  Future<void> onRefresh() async{
    ipEliteList.value = [];
    query['pageCode'] = 1;
    update();
    fnGet();
  }
}
